package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.keyboard.Keyboard;
import com.yiwei.zxp.view.keyboard.PayEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class SecurityPassDilaog extends Dialog {


    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.pay_edit_text)
    PayEditText payEditText;
    @BindView(R.id.keyboard)
    Keyboard keyboard;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    private Context context;
    private static final String[] KEY = new String[]{
            "1", "2", "3",
            "4", "5", "6",
            "7", "8", "9",
            "<<", "0", "完成"
    };

    public SecurityPassDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public SecurityPassDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }


    private void initView() {
        setContentView(R.layout.dialog_security_pass);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);
        keyboard.setKeyboardKeys(KEY);
//键盘键的点击事件
        keyboard.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
            @Override
            public void onKeyClick(int position, String value) {
                if (position < 11 && position != 9) {
                    payEditText.add(value);
                } else if (position == 9) {
                    payEditText.remove();
                } else if (position == 11) {
                    //当点击d键盘上的完成时候，也可以通过payEditText.getText()获取密码，此时不应该注册OnInputFinishedListener接口
                    if (payEditText.getText().length() < 6) {
                        ToastUtil.makeText(context, "请输入6位交易密码");
                        return;
                    }
                    keyboard.setVisibility(View.GONE);
                    if (onChooseListener != null) {
                        onChooseListener.onCustomParamChoose(payEditText.getText());
                    }
                }
            }
        });

//当密码输入完成时的回调
        payEditText.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String password) {
                keyboard.setVisibility(View.GONE);
                if (onChooseListener != null) {
                    onChooseListener.onCustomParamChoose(payEditText.getText());
                }
                dismiss();
            }

            @Override
            public void onClick() {
                keyboard.setVisibility(View.VISIBLE);
            }
        });
    }


    public SecurityPassDilaog setOrderMoney(String money) {
        txtMoney.setText(money);
        return this;
    }

    private OnChooseListener onChooseListener;

    public SecurityPassDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    @OnClick(R.id.btn_close)
    public void onViewClicked() {
        dismiss();
    }

    public interface OnChooseListener {
        void onCustomParamChoose(String pass);
    }
}
